<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>按钮切换active</title>
    <style>
        .active {
            background-color: blue;
            color: white;
        }
    </style>
</head>
<body>
<div class="b">
    <button class="active">按钮1</button>
    <button>按钮2</button>
    <button>按钮3</button>
</div>

<script>
    const buttons = document.querySelectorAll(".b button");
    buttons.forEach((btn, index) => {
        btn.addEventListener("click", () => {
            buttons.forEach((b, i) => b.classList.toggle("active", i === index));
        });
    });
</script>
</body>
</html>
